package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.MmrdT;
import cn.e4j.modular.crm.entity.MmrfT;
import cn.e4j.modular.crm.entity.MmrnT;
import cn.e4j.modular.crm.mapper.MmrdTMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务预约维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-04-03
 */
@Service
public class MmrdTServiceImpl extends ServiceImpl<MmrdTMapper, MmrdT> {

//    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmrdList(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> mmrdDtos = this.baseMapper.getListData(page, dto);
        List<Map<String, Object>> mmrdDtos = InvokeUtil.invokeBeanMethod("mmrdTMapper,getListData",page,dto);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmraList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> mmraDtos = this.baseMapper.getMmraList(page, dto);
        return mmraDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getUsers(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = Dtos.newDto();
        inDto.put("ORG_ID", shiroUser.getORG_ID());
        //List<Dto> orgList = this.baseMapper.getOrgList(inDto);
        List<Dto> orgList = InvokeUtil.invokeBeanMethod("mmrdTMapper,getOrgList",inDto);
        if(orgList.size() > 0){
            dto.put("orgList", orgList);
        }
        //List<Map<String, Object>> userDtos = this.baseMapper.getUsers(dto);
        List<Map<String, Object>> userDtos =InvokeUtil.invokeBeanMethod("mmrdTMapper,getUsers",dto);
        return userDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmrd013List(Dto dto) {
        //List<Map<String, Object>> mmrdDtos = this.baseMapper.getMmrd013List(dto);
        List<Map<String, Object>> mmrdDtos = InvokeUtil.invokeBeanMethod("mmrdTMapper,getMmrd013List",dto);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmrddocnoPage(Page<Object> page, Dto dto) {
        List<Map<String, Object>> mmrdDtos = this.baseMapper.getMmrddocnoPage(page, dto);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getStarList(Dto dto) {
        List<Map<String, Object>> mmrjDtos = this.baseMapper.getStarList(dto);
        return mmrjDtos;
    }

    @Transactional(readOnly = true)
    public List<MmrdT> getData(MmrdT mmrdT) {
        QueryWrapper<MmrdT> mmrdTUpdateWrapper = new QueryWrapper<>();
        mmrdTUpdateWrapper.eq("MMRD010",mmrdT.getMMRD010());
        mmrdTUpdateWrapper.eq("MMRD011",mmrdT.getMMRD011());
        mmrdTUpdateWrapper.eq("MMRD012",mmrdT.getMMRD012());
        mmrdTUpdateWrapper.eq("MMRD001",mmrdT.getMMRD001());
        mmrdTUpdateWrapper.eq("MMRD002",mmrdT.getMMRD002());
        mmrdTUpdateWrapper.eq("MMRD003",mmrdT.getMMRD003());
        mmrdTUpdateWrapper.eq("MMRD013",mmrdT.getMMRD013());
        mmrdTUpdateWrapper.eq("MMRD014",mmrdT.getMMRD014());
        //mmrdTUpdateWrapper.eq("MMRD004",mmrdT.getMMRD004());
        mmrdTUpdateWrapper.eq("MMRD005",mmrdT.getMMRD005());
        mmrdTUpdateWrapper.eq("MMRD019",mmrdT.getMMRD019());
        mmrdTUpdateWrapper.eq("MMRD020",mmrdT.getMMRD020());
        List<MmrdT> mmrdDtos = this.baseMapper.selectList(mmrdTUpdateWrapper);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getData2(Dto param) {
        List<Dto> mmrdDtos = baseMapper.getData2(param);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public Dto selectGwak23_25(Dto dto) {
        Dto gwakDtos = this.baseMapper.selectGwak23_25(dto);
        return gwakDtos;
    }

    @Transactional
    public void add(MmrdT mmrdT) {
        //mmrdT.setMMRDDOCNO("WXFW" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        //mmrdT.setMMRDSEQ(new BigDecimal(1));
        mmrdT.setMMRDDOCDATE(LocalDate.now());
        mmrdT.setMMRD016("3");
        mmrdT.setMMRDACTI("Y");
        mmrdT.insert();
    }

    @Transactional
    public Integer edit(MmrdT mmrdT, Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        UpdateWrapper<MmrdT> mmrdTUpdateWrapper = new UpdateWrapper<>();
        mmrdTUpdateWrapper.eq("MMRDENT",mmrdT.getMMRDENT());
        mmrdTUpdateWrapper.eq("MMRDDOCNO",mmrdT.getMMRDDOCNO());
        mmrdTUpdateWrapper.eq("MMRDSEQ",mmrdT.getMMRDSEQ());
        if("shouli".equals(inDto.getString("operation"))){
            mmrdT.setMMRD020("2");
            mmrdT.setMMRD021(shiroUser.getID());
        }
        if("paigong".equals(inDto.getString("operation"))){
            mmrdT.setMMRD020("3");
            MmrfT mmrfT = this.generateMmrf(mmrdT,inDto);
            mmrfT.insert();
        }
        if("jiean".equals(inDto.getString("operation")) || "huifang".equals(inDto.getString("operation"))){
            mmrdT.setMMRD020("6");
            mmrdT.setMMRD027(LocalDate.now());
            if("huifang".equals(inDto.getString("operation"))){
                List<MmrnT> mmrnTList = this.generateMmrn(mmrdT,inDto);
                for(MmrnT mmrnT: mmrnTList){
                    mmrnT.insert();
                }
            }
        }
        if("edit".equals(inDto.getString("operation"))){
            InvokeUtil.invokeBeanMethod("mmrdTMapper,updateMmrd", inDto);
        }else {
            mmrdT.update(mmrdTUpdateWrapper);
        }

        return 200;
    }

    @Transactional
    public void delete(MmrdT mmrdT) {
        UpdateWrapper<MmrdT> mmrdTUpdateWrapper = new UpdateWrapper<>();
        mmrdTUpdateWrapper.eq("MMRDENT",mmrdT.getMMRDENT());
        mmrdTUpdateWrapper.eq("MMRDDOCNO",mmrdT.getMMRDDOCNO());
        mmrdTUpdateWrapper.eq("MMRDSEQ",mmrdT.getMMRDSEQ());
        mmrdT.delete(mmrdTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<MmrdT> mmrdTList) {
        for (MmrdT mmrdT : mmrdTList) {
            UpdateWrapper<MmrdT> mmrdTUpdateWrapper = new UpdateWrapper<>();
            mmrdTUpdateWrapper.eq("MMRDENT",mmrdT.getMMRDENT());
            mmrdTUpdateWrapper.eq("MMRDDOCNO",mmrdT.getMMRDDOCNO());
            mmrdTUpdateWrapper.eq("MMRDSEQ",mmrdT.getMMRDSEQ());

            mmrdT.delete(mmrdTUpdateWrapper);
        }
    }

    private MmrfT generateMmrf(MmrdT mmrdT, Dto inDto){
        MmrfT mmrfT = new MmrfT();
        mmrfT.setMMRFENT(inDto.getString("ENT"));
        mmrfT.setMMRFDOCNO(inDto.getString("MMRFDOCNO"));
        mmrfT.setMMRFSEQ(inDto.getBigDecimal("MMRFSEQ"));
        mmrfT.setMMRFDOCDATE(LocalDate.now());
        mmrfT.setMMRF001(mmrdT.getMMRDDOCNO());
        mmrfT.setMMRF002(mmrdT.getMMRDSEQ());
        mmrfT.setMMRF003(mmrdT.getMMRD002());
        mmrfT.setMMRF004(mmrdT.getMMRD003());
        mmrfT.setMMRF005("*");
        mmrfT.setMMRF007(inDto.getString("MMRF007"));

        Dto user = this.baseMapper.getUserByID(inDto.getString("MMRF007"));
        mmrfT.setMMRF006(user.getString("ORG_ID"));
        mmrfT.setMMRF008(LocalDate.parse(inDto.getString("MMRF008")));
        mmrfT.setMMRFACTI("Y");
        return mmrfT;
    }

    private List<MmrnT> generateMmrn(MmrdT mmrdT, Dto inDto){
        List<MmrnT> mmrnTList = new ArrayList<>();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<MmrnT> list = JSONObject.parseArray(jsonObject.getString("list"), MmrnT.class);
        for(int i=0;i< list.size();i++){
            MmrnT mmrnT = new MmrnT();
            mmrnT.setMMRNENT(inDto.getString("ENT"));
            mmrnT.setMMRNDOCNO(inDto.getString("MMRNDOCNO"));
            mmrnT.setMMRNSEQ(new BigDecimal(i).add(new BigDecimal(1)));
            //mmrnT.setMMRNDOCDATE(DateUtils.parseDate(DateUtils.formatDate(new Date(),DateUtils.PATTERN_yyyy_MM_dd)));
            mmrnT.setMMRNDOCDATE(LocalDate.now());
            mmrnT.setMMRN001(mmrdT.getMMRDDOCNO());
            mmrnT.setMMRN002(mmrdT.getMMRDSEQ().toString());
            mmrnT.setMMRN002(mmrdT.getMMRDSEQ().toString());
            mmrnT.setMMRN005(list.get(i).getMMRN005());

            mmrnT.setMMRN006(mmrdT.getMMRD015());
            mmrnT.setMMRN007("N"); //默认不是匿名回访

            mmrnT.setMMRN008(list.get(i).getMMRN008());
            mmrnT.setMMRN009(list.get(i).getMMRN009());
            mmrnT.setMMRN010(list.get(i).getMMRN010());

            mmrnT.setMMRNACTI("Y");
            mmrnT.setMMRN003(list.get(i).getMMRN003()); //指标编号
            mmrnT.setMMRN004(list.get(i).getMMRN004()); //指标星数

            mmrnTList.add(mmrnT);
        }
        return mmrnTList;
    }

    @Transactional(readOnly = true)
    public Dto getMMRF(Dto dto) {
        List<Dto> mmrflist = this.baseMapper.getMMRF(dto);
        Dto mmrfDtos = Dtos.newDto();
        if(mmrflist.size() > 0){
            mmrfDtos = mmrflist.get(0);
            if(mmrfDtos.getString("MMRG102") != null){
                mmrfDtos.put("MMRG102", mmrfDtos.getString("MMRF102").replace("\n"," "));
            }
        }
        return mmrfDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMMRN(Dto dto) {
        List<Map<String, Object>> mmrnDtos = this.baseMapper.getMMRN(dto);
        return mmrnDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmrddocno(Page<Object> page, Dto dto) {
        List<Map<String, Object>> mmrdDtos = this.baseMapper.getMmrddocno(page, dto);
        return mmrdDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMmrq001(Dto dto) {
        List<Dto> mmrq001Dtos = this.baseMapper.getMmrq001(dto);
        return mmrq001Dtos;
    }
}
